
import pymongo
from contextlib import contextmanager


class MongoDB:
    def __init__(self, host, port, db):
        self.host = host
        self.port = port
        self.db = db

    @contextmanager
    def get_client(self):
        mg = pymongo.MongoClient(self.host, self.port)
        try:
            dbs = mg.get_database(self.db)
            yield dbs
        except Exception as e:
            print(e)
        finally:
            mg.close()

mg = MongoDB(host='127.0.0.1', port=27017, db="test_demo")

with mg.get_client() as mogo:
    client = mogo.get_collection("test")
    res = client.delete_many({"adc":"lll"})
    print(res.deleted_count)


myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['pctt']
mycol = mydb['lg']

document = mycol.find()

for x in mycol.find():
    print(x)

document = mycol.find({'图号':{ "$regex": "UZ11601" }})

for i in document:
    print(i)